package com.base.controller;

import com.base.po.tbk.TbkChoiceGoods;
import com.base.po.voip.Users;
import com.base.service.TbkChoiceGoodsService;
import com.base.service.UsersService;
import com.base.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 精选单品
 *
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class index_ChoiceGoods {
	@Autowired
	private TbkChoiceGoodsService tbkChoiceGoodsService;
	@Autowired
	private UsersService usersService;

	// 精选单品首页
	@RequestMapping("/choiceGoodsIndex")
	public ModelAndView index(HttpServletRequest request, Integer type) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		Page page = new Page();
		Users users = usersService.selectByPrimaryKey(adminUserId);
		List<TbkChoiceGoods> tbkChoiceGoods = null;

		page.setCurrentPageNo(1);
		page.setRecordTotal(tbkChoiceGoodsService.countAll(adminUserId, type));
		tbkChoiceGoods = tbkChoiceGoodsService.selectAll(page, adminUserId, type);

		request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
		request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
		request.setAttribute("tbkChoiceGoods", tbkChoiceGoods);
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/index_ChoiceGoods");
		return modelAndView;
	}

	@RequestMapping("/selectChoiceGoods")
	public ModelAndView selectChoiceGoods(HttpServletRequest request, Integer curPage, Integer type) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}

		String adminUserId = (String) request.getSession().getAttribute("adminUserId");

		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);

		List<TbkChoiceGoods> tbkChoiceGoods = null;

        page.setCurrentPageNo(curPage);
        page.setRecordTotal(tbkChoiceGoodsService.countAll(adminUserId, type));
		tbkChoiceGoods = tbkChoiceGoodsService.selectAll(page, adminUserId, type);

        request.setAttribute("totlenum", page.getRecordTotal());// 传入session中
        request.setAttribute("curPage", page.getCurrentPageNo());// 传入session中
        request.setAttribute("tbkChoiceGoods", tbkChoiceGoods);
		request.setAttribute("type", type);
        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_ChoiceGoods");
		return modelAndView;
	}
}
